package com.demo2do.boyle.client.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.client.dao.TaskDaoSupport;
import com.demo2do.boyle.client.service.TaskService;
import com.demo2do.boyle.common.entity.advertisement.Advertisement;
import com.demo2do.boyle.common.entity.advertisement.Status;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.entity.system.Variable;
import com.demo2do.boyle.common.entity.task.Task;
import com.demo2do.boyle.common.entity.task.TaskStatus;
import com.demo2do.core.entity.Result;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.demo2do.core.web.resolver.Page;

/**
 * @author mohanlan
 */
@Service("taskService")
public class TaskServiceImpl extends GenericServiceImpl<Task> implements TaskService{
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@Autowired
	private TaskDaoSupport taskDaoSupport;

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.TaskService#listByCustomer(com.demo2do.boyle.common.entity.customer.Customer, com.demo2do.boyle.common.entity.task.TaskStatus, com.demo2do.core.web.resolver.Page)
	 */
	@SuppressWarnings("unchecked")
	public List<Task> listByCustomer(Customer customer, String status, Page page) {
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("customer", customer);
		
		String sentence = "FROM Task task WHERE task.customer = :customer";
		
		if(status != null) {
			
			if ("CLAIMED".equals(status)) {
				
				parameters.put("taskStatus", new TaskStatus[]{TaskStatus.CLAIMED,TaskStatus.DONE,TaskStatus.UNPASS});
				sentence = sentence + " AND task.taskStatus in :taskStatus";
				
			} else if("FINISH".equals(status)) {
				
				parameters.put("taskStatus", TaskStatus.FINISH);
				sentence = sentence + " AND task.taskStatus = :taskStatus";
			}
			
		}
		
		parameters.put("status", Status.HANDLING);
		parameters.put("endDate", new Date());
		sentence = sentence + " AND task.advertisement.status >= :status AND task.advertisement.endDate >= :endDate ";
		
		List<Task> result = genericDaoSupport.searchForList(sentence , parameters, page.getBeginIndex(), page.getMaxResultRecords());
		
		return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.TaskService#create(com.demo2do.boyle.common.entity.customer.Customer, com.demo2do.boyle.common.entity.advertisement.Advertisement)
	 */
	public Result create(Customer customer, Advertisement advertisement,Variable variable) {
		
		Result result = new Result();
		
		// check if the task has been created
		if(taskDaoSupport.loadTask(customer, advertisement) != null) {
			result.fail().message("任务已被认领，请去个人中心查看！");
		}else {
			Task task = new Task(customer, advertisement);
			task.setReward(Integer.valueOf(variable.getValue())*100);
			genericDaoSupport.save(task.initialize());
			result.success().message("创建任务成功");
		}
		
		return result;
	}

}
